import os
import numpy as np
import joblib
import torch
from Model import Model

class LSTM:
    def __init__(self) -> None:
        self.model = Model()
        cwd = os.getcwd()
        model_path = os.path.join(cwd,"LSTM/model2.pth")
        model2_path = os.path.join(cwd,"LSTM/MinMaxScaler.pkl")
        model_status = torch.load(model_path,map_location='cpu')
        self.model.load_state_dict(model_status)
        self.transer = joblib.load(model2_path)

    def predict(self,data:list) -> float:
        """
            input : list [x1,x2,x3]
            return : float y4
        """
        self.data = np.array(data).ravel()
        self.data = self.transer.transform(self.data.reshape(-1,1)).ravel()
        if self.data.__len__() <= 8:
            self.data = self.data.reshape((1,-1,1))
            self.data = torch.tensor(self.data,dtype=torch.float32)
        else:
            self.data = self.data.reshape((-1,1))
            self.data = [self.data[len(self.data)-8:len(self.data)].tolist()]
            self.data = torch.tensor(self.data,dtype=torch.float32)
        
        # ret = self.model(self.data).item()
        ret = self.model(self.data).detach().numpy()
        ret = self.transer.inverse_transform(ret.reshape((-1,1)))
        ret = ret[0][0]
        return ret
if __name__ == "__main__":
    # example
    lstm = LSTM()
    lst = [1,2,3,4,5,6,7,8,9,10,11]
    print(lstm.predict(lst))